package com.mingmen.canting.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;


import com.mingmen.canting.R;
import com.mingmen.canting.model.CaiGouRightModel;
import com.mingmen.canting.model.GoodsBean;
import com.mingmen.canting.model.MyShopBean;
import com.mingmen.canting.model.NormalBean;
import com.mingmen.canting.model.ShoppingCartModel;
import com.mingmen.canting.viewholder.ChildViewHolder;
import com.mingmen.canting.viewholder.GroupViewHolder;
import com.mingmen.canting.viewholder.NormalViewHolder;
import com.ocnyang.cartlayout.CartAdapter;
import com.ocnyang.cartlayout.bean.CartItemBean;
import com.ocnyang.cartlayout.bean.ICartItem;
import com.ocnyang.cartlayout.viewholder.CartViewHolder;

import java.util.ArrayList;
import java.util.List;

public class CaiGouMainAdapter extends CartAdapter<CartViewHolder> {
    List datas = new ArrayList();
    Context context;
    List<ShoppingCartModel.DataBean.SupDataListBean.DetailsBean> detailsBeanList;

    public CaiGouMainAdapter(Context context, List datas, List<ShoppingCartModel.DataBean.SupDataListBean.DetailsBean> detailsBeanList) {
        super(context, datas);
        this.context = context;
        this.datas = datas;
        this.detailsBeanList = detailsBeanList;
    }

    @Override
    protected CartViewHolder getNormalViewHolder(View itemView) {
        return new NormalViewHolder(itemView, -1);
    }

    @Override
    protected CartViewHolder getGroupViewHolder(View itemView) {
        return (CartViewHolder) new GroupViewHolder(itemView, R.id.checkbox);
    }

    @Override
    protected CartViewHolder getChildViewHolder(View itemView) {
        return (CartViewHolder) (new ChildViewHolder(itemView, R.id.checkbox) {
            @Override
            public void onNeedCalculate() {
                if (onCheckChangeListener != null) {
                    onCheckChangeListener.onCalculateChanged(null);
                }
            }

            @Override
            public void onFangqi() {

            }

        });
    }


    @Override
    protected int getChildItemLayout() {
        return R.layout.activity_main_item_child;
    }

    @Override
    protected int getGroupItemLayout() {
        return R.layout.activity_main_item_group;
    }

    @Override
    protected int getNormalItemLayout() {
        return R.layout.activity_main_item_normal;
    }

    public void setOnitemClickLintener(ChilditemClick onitemClick) {
        this.onitemClick = onitemClick;
    }

    String applyDetailId;
    String cancelFlag;

    @Override
    public void onBindViewHolder(@NonNull CartViewHolder holder, final int position) {
        super.onBindViewHolder(holder, position);
        if (holder instanceof GroupViewHolder) {
            GroupViewHolder groupViewHolder = (GroupViewHolder) holder;

            groupViewHolder.textView.setText(((ShoppingCartModel.DataBean.SupDataListBean) mDatas.get(position)).getSupplierName());
            Log.d("getSupplierName", ((ShoppingCartModel.DataBean.SupDataListBean) mDatas.get(position)).getSupplierName());
            if (((ShoppingCartModel.DataBean.SupDataListBean) mDatas.get(position)).getSupplierName().equals("")) {
                groupViewHolder.textView.setVisibility(View.GONE);
                groupViewHolder.img_sanjiao.setVisibility(View.GONE);
                groupViewHolder.text_price.setVisibility(View.GONE);
                groupViewHolder.view_lines.setVisibility(View.GONE);
            } else {
                groupViewHolder.textView.setVisibility(View.VISIBLE);
                groupViewHolder.img_sanjiao.setVisibility(View.VISIBLE);
                groupViewHolder.text_price.setVisibility(View.VISIBLE);
                groupViewHolder.view_lines.setVisibility(View.VISIBLE);
            }
            groupViewHolder.layout_selectshouhuo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mGroutClick.onItemClick(v, position);
                }
            });
            groupViewHolder.sele_gongYing.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mGroutClick.onItemClick(v, position);
                }
            });
        } else if (holder instanceof ChildViewHolder) {
            ChildViewHolder childViewHolder = (ChildViewHolder) holder;
            List<String> receive = new ArrayList<>();
            childViewHolder.textView.setText(((ShoppingCartModel.DataBean.SupDataListBean.DetailsBean) mDatas.get(position)).getCommodityName());
            childViewHolder.applyAmount.setText(((ShoppingCartModel.DataBean.SupDataListBean.DetailsBean) mDatas.get(position)).getApplyAmount() + "");
            childViewHolder.specs.setText(((ShoppingCartModel.DataBean.SupDataListBean.DetailsBean) mDatas.get(position)).getSpecs() + "");

            if (((ShoppingCartModel.DataBean.SupDataListBean.DetailsBean) mDatas.get(position)).getSupplierName().equals("")) {
                childViewHolder.price.setVisibility(View.GONE);
                childViewHolder.view_lines.setVisibility(View.GONE);
            } else {
                childViewHolder.price.setVisibility(View.VISIBLE);
                childViewHolder.view_lines.setVisibility(View.VISIBLE);
            }
            childViewHolder.price.setText(((ShoppingCartModel.DataBean.SupDataListBean.DetailsBean) mDatas.get(position)).getPrice() + "");
            cancelFlag = ((ShoppingCartModel.DataBean.SupDataListBean.DetailsBean) mDatas.get(position)).getCancelFlag();
//        getData();
            for (int i = 0; i < mDatas.size(); i++) {
                receive.add(((ShoppingCartModel.DataBean.SupDataListBean.DetailsBean) mDatas.get(position)).getCategoryName());
            }


            if (mDatas.size() > 1) {
                if (position == mDatas.size() - 1) {
                    childViewHolder.layout_child_bck.setBackground(context.getResources().getDrawable(R.drawable.shape_caigou_child));
                } else {
                    childViewHolder.layout_child_bck.setBackground(context.getResources().getDrawable(R.drawable.shape_caigou_child2));
                }
            } else {
                childViewHolder.layout_child_bck.setBackground(context.getResources().getDrawable(R.drawable.shape_caigou_child));
            }

            Log.d("mDatassize", receive.size() + "");

            if (cancelFlag.equals("0")) {
                childViewHolder.text_fangqi.setText("放弃");
                childViewHolder.text_fangqi.setTextColor(mContext.getResources().getColor(R.color.red_textcolor));
            } else {
                childViewHolder.text_fangqi.setText("恢复");
                childViewHolder.text_fangqi.setTextColor(mContext.getResources().getColor(R.color.normal_store));
            }
            childViewHolder.text_fangqi.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cancelFlag = ((ShoppingCartModel.DataBean.SupDataListBean.DetailsBean) mDatas.get(position)).getCancelFlag();
                    applyDetailId = ((ShoppingCartModel.DataBean.SupDataListBean.DetailsBean) mDatas.get(position)).getApplyDetailId();
                    onitemClick.onItemClick(v, position, mDatas, applyDetailId, cancelFlag);
                }
            });

        } else if (holder instanceof NormalViewHolder) {
            NormalViewHolder normalViewHolder = (NormalViewHolder) holder;
            normalViewHolder.imgViewClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDatas.remove(position);
                    notifyItemRemoved(position);
                    notifyItemRangeChanged(position, mDatas.size());
                }
            });
            normalViewHolder.textView.setText(mContext.getString(R.string.normal_tip_X,
                    ((NormalBean) mDatas.get(position)).getMarkdownNumber()));
        }


    }

    //定义一个点击事件的接口
    public interface ChilditemClick {
        void onItemClick(View view, int position, List<ICartItem> mDatas, String applyDetailId, String cancelFlag);
    }

    public interface GroupItemClick {
        void onItemClick(View view, int position);
    }

    private GroupItemClick mGroutClick;   //定义点击事件接口
    private ChilditemClick onitemClick;   //定义点击事件接口

    public void setonGroupItemLinstener(GroupItemClick onitemClick) {
        this.mGroutClick = onitemClick;
    }
}
